<template>
    <div class='post'>
        <div class="container">
            <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" class="search-con">
                <el-form-item label="账号等级" prop="key_level" style="margin-right:25px;">
                    <el-select v-model="queryParams.key_level" placeholder="请选择" clearable size="small">
                        <el-option v-for="dict in positionList" :key="dict.dictValue" :label="dict.dictLabel"
                            :value="dict.dictValue" />
                    </el-select>
                </el-form-item>

                <el-form-item label="状态" prop="key_status" style="margin-right:25px;">
                    <el-select v-model="queryParams.key_status" placeholder="请选择" clearable size="small">
                        <el-option v-for="dict in statusList" :key="dict.dictValue" :label="dict.dictLabel"
                            :value="dict.dictValue" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>

            <el-row :gutter="10" class="mb8">
                <el-col :span="22">
                    <!-- <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleBtn('add')"
                        v-hasPermi="['user:post:add']">
                        新增</el-button> -->
                    <el-button type="primary" icon="el-icon-lx-settings" size="mini" @click="handleSettng('setting')"
                        v-hasPermi="['user:post:add']">
                        分销设置</el-button>
                </el-col>

                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table border v-loading="loading" :data="tableList" ref="tableList" v-tableHeight
                :height="tableList && tableList.length > 0 ? '900px' : '120px'">
                <el-table-column label="序号" align="center" prop="id" />
                <el-table-column label="用户名" align="center" prop="name" />
                <el-table-column label="默认会员等级" align="center" prop="name" />
                <el-table-column label="注册时间" align="center" prop="create_time" />
                <el-table-column label="营业额" align="center" prop="name" />
                <el-table-column label="拉新人数" align="center" prop="name" />
                <el-table-column label="所属上级" align="center" prop="name" />
                <el-table-column label="所属下级" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" icon="el-icon-lx-calendar" @click="handleDetail(scope.row);"
                            v-hasPermi="['user:post:edit']">
                            详情
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center" prop="status">
                    <template slot-scope="scope">
                        <span>
                            {{ selectDictLabel(statusList, scope.row.status) }}
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <!-- <el-button size="mini" type="text" icon="el-icon-s-order" @click="setPermissions(scope.row)" v-hasPermi="['user:post:permission']">
                            菜单权限
                        </el-button> -->
                        <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row);"
                            v-hasPermi="['user:post:edit']">
                            编辑
                        </el-button>
                        <el-button v-if="scope.row.status == 0" size="mini" type="text" @click="handleAble(scope.row)"
                            v-hasPermi="['user:post:stop']">停用
                        </el-button>
                        <el-button v-if="scope.row.status == 1" size="mini" type="text" @click="handleAble(scope.row)"
                            v-hasPermi="['user:post:start']">启用
                        </el-button>
                        <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                            v-hasPermi="['user:post:del']">
                            删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="pageCount > 0" :pageCount="pageCount" :page.sync="queryParams.page"
                :limit.sync="queryParams.limit" @pagination="getList" />
        </div>
        <subList v-if="handleShow == 'sub'" :title="title" :handleShow="handleShow" :distId="distId"
            @close="userId = null; handleShow = ''" @load="getList" />
        <dsbsetting v-if="handleShow == 'setting'" :title="handleShow == 'setting' ? '分销设置' : '分销设置'" :handleShow="handleShow" @close="handleShow = ''" @load="getList" />
        <el-dialog :title="handleShow == 'add' ? '新增' : '编辑'" visible v-if="handleShow == 'edit'" @close="close()" width="900px"
            append-to-body :close-on-click-modal="false">
            <el-form ref="form" :model="form" :rules="rules" label-width="120px" v-loading="formLoading">
                <el-row>
                    <el-col :span="24">
                                <el-form-item label="分销员姓名:" prop="name">
                                    <span>{{ form.name }}</span>
                                </el-form-item>
                            </el-col>
                    <el-col :span="24">
                        <el-form-item label="账号等级:" prop="level">
                            <el-select v-model="form.level" placeholder="请输入选择" clearable size="small" filterable style="width: 300px;">
                                <el-option v-for="dict in positionList" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                                <el-form-item label="注册时间:" prop="create_time">
                                    <span>{{ form.create_time }}</span>
                                </el-form-item>
                            </el-col>
                    <el-col :span="24">
                        <el-form-item label="所属上级:" prop="one_superior">
                            <el-select v-model="form.one_superior" placeholder="请输入选择" clearable size="small" filterable style="width: 700px;">
                                <el-option v-for="dict in positionList" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="备注" prop="note">
                            <el-input v-model="form.note" type="textarea" rows="3" placeholder="请输入"></el-input>
                        </el-form-item>
                    </el-col>
                    <!-- </template> -->
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="close()">取 消</el-button>
                <el-button type="primary" @click="submit" :disabled="formLoading">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import { getDistLists, getDistInfo, setDistInfo, setDistStatus } from "@/api/distribution/distribution.js"
import subList from "./handle/subList"
import dsbsetting from "./handle/distriSetting"
// import { setAuth, getAuth, getAuthMenusLists } from "@/api/common.js"
import { departStatus, positionList  } from "@/api/dictionary"
// import {
//     saveObjArr,
//     getObjArr
// } from "@/utils/ruoyi.js"
import throttle from 'lodash.throttle'
export default {
    name: 'post',
    props: {

    },
    components: {
        subList,
        dsbsetting
    },
    data() {
        return {
            showSearch: true,
            loading: false,
            pageCount: 0,
            total: 0,
            queryParams: {
                page: 1,
                limit: 20,
                key_level:'',
                key_status:'',
            },
            tableList: [
                {id:1, name:'张三', create_time:'2023年8月3日10:25:01'}
            ],
            statusList: departStatus,
            positionList:positionList,
            handleShow: "",
            formLoading: false,
            form: {
                dist_id: null,
                name: null,
            },
            rules: {
                level: [{ required: true, message: "请选择等级", trigger: "change" }],
                one_superior: [{ required: true, message: '请选择所属上级', trigger: 'blur' }],
            },
            distId:'',
        }
    },
    created() {   
            // this.getList()
    },
    methods: {
        getList() {
            this.loading = true;
            getDistLists(this.queryParams).then(res => {
                this.tableList = res.data.lists;
                this.pageCount = res.data.pageCount;
                this.loading = false;
            });
        },
        handleQuery() {
            this.queryParams.page = 1;
            this.getList();
        },
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        close() {
            this.form = {}
            this.handleShow = ""
        },
        handleBtn(type, row) {
            getDistInfo({dist_id:row.id}).then(res => {
                console.log(res,'res1111111')
                // this.depList = res.data
            })
            if (row) {
                this.userId = row.id
                // this.form = JSON.parse(JSON.stringify(row));
            }
            this.handleShow = type
        },
        handleSettng(){
            this.handleShow = 'setting';
            this.title = '分销员设置';
        },
        handleEdit(row){
            this.handleShow = 'edit';
            this.title = '分销员编辑';
            this.form.dist_id = row.id;
            this.form.name = row.name;
            this.form.create_time = row.create_time;
        },
        handleAble(row) {
            let label = row.status == 0 ? '停用' : '启用';
            this.$confirm(`是否将“${row.name}”${label}?`, "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return setDistStatus({ dist_id: row.id });
            }).then(() => {
                this.getList();
                this.msgSuccess(`${label}成功`);
            })
        },
        // handleDelete(row) {
        //     this.$confirm('是否确认删除' + '"' + row.name + '"' + '?', "警告", {
        //         confirmButtonText: "确定",
        //         cancelButtonText: "取消",
        //         type: "warning"
        //     }).then(function () {
        //         return delJobs({ jobs_id: row.id });
        //     }).then(() => {
        //         this.getList();
        //         this.msgSuccess("删除成功");
        //     })
        // },
        handleDetail(row) {
            this.handleShow = 'sub';
            this.title = '分销员下级列表';
            this.distId = row.id

        },
        // // 树权限（展开/折叠）
        // handleCheckedTreeExpand(value) {
        //     let treeList = this.menuOptions;
        //     for (let i = 0; i < treeList.length; i++) {
        //         this.$refs.menu.store.nodesMap[treeList[i].id].expanded = value;
        //     }
        // },
        // // 树权限（全选/全不选）
        // handleCheckedTreeNodeAll(value) {
        //     this.$refs.menu.setCheckedNodes(value ? this.menuOptions : []);
        // },
        // // 树权限（父子联动）
        // handleCheckedTreeConnect(value) {
        //     this.form.menuCheckStrictly = value ? true : false;
        // },
        // // 所有菜单节点数据
        // getMenuAllCheckedKeys() {
        //     // 目前被选中的菜单节点
        //     let checkedKeys = this.$refs.menu.getCheckedKeys();
        //     // 半选中的菜单节点
        //     let halfCheckedKeys = this.$refs.menu.getHalfCheckedKeys();
        //     checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
        //     return checkedKeys;
        // },
        // setPermissions(row) {
        //     getAuth({ job_id: row.id }).then(res => {
        //         this.$nextTick(() => {
        //             this.defaultKeys = res.data.split(",")
        //         })
        //         this.form.job_id = row.id
        //         this.title = '菜单权限';
        //         this.handleShow = 'menu'
        //     })
        // },

        submit: throttle(function () {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.formLoading = true;
                    setDistInfo(this.form).then(res => {
                        this.msgSuccess("操作成功！");
                        this.close()
                        this.formLoading = false;
                        this.$emit('load')
                    }).catch(() => {
                        this.formLoading = false;
                    })
                }
            })
        }, 1000, { trailing: false }),
    }
}
</script>

<style lang='scss' scoped></style>
